package net.wanho.controller;

import lombok.SneakyThrows;
import net.wanho.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-12-12 15:31
 * Description：<描述>
 */
@RestController
public class ParamController {

    /**
     * JavaEE组件
     */
    @SneakyThrows
    @RequestMapping("/param1")
    public void param1(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("name");
        String method = req.getMethod();
        String userAgent = req.getHeader("User-Agent");
        System.out.println(name);
        System.out.println(method);
        System.out.println(userAgent);

        resp.setContentType("text/html; charset=utf8");
        PrintWriter out = resp.getWriter();
        out.print("<h2>嘿嘿</h2>");
        out.close();
    }

    /**
     * String、基本类型及包装类
     */
    @RequestMapping("/param2")
    public String param2(@RequestParam("username") String name, @RequestParam(required = false, defaultValue = "18") int age, Double height){
        System.out.println(name + ":" + age + ":" + height);
        return "success";
    }

    @RequestMapping("/param3/{name}/{userAge}")
    public String param3(@PathVariable String name, @PathVariable("userAge") int age){
        System.out.println(name + ":" + age);
        return "success";
    }

    @RequestMapping("/param4")
    public String param3(@RequestHeader("User-Agent") String userAgent){
        System.out.println(userAgent);
        return "success";
    }

    @RequestMapping("/param5")
    public String param5(@CookieValue String address){
        System.out.println(address);
        return "success";
    }

    @RequestMapping("/param6")
    // public String param6(@RequestBody String args){
    // public String param6(@RequestBody User user){
    // public String param6(@RequestBody Integer[] ids){
    // public String param6(@RequestBody List<Integer> ids){
    public String param6(@RequestBody List<User> list){
        // System.out.println(args);
        // System.out.println(user);
        // System.out.println(ids);
        System.out.println(list);
        return "success";
    }

    /**
     * 自定义对象类型
     */
    @RequestMapping("/param7")
    public String param7(User user){
        System.out.println(user);
        return "success";
    }

}
